﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;
using SBACN.WebSite.Models;
using SBACN.WebSite.Code;
using SBACN.WebSite.Cache;

namespace SBACN.WebSite.Admin.Feedback
{
    public partial class ComplainList : AdminBasePage
    {
        public string SystemType
        {
            get
            {
				//if (ViewState["SystemType"] == null) return "00090003";
				//else return ViewState["SystemType"].ToString();
				return this.SystemCode;
            }
            private set
            {
				ViewState["SystemType"] = this.SystemCode;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //exDataBind(this.dpl_Type, SystemMapCache.Instance.List.Where(p => p.Code.StartsWith(SystemType) && p.Code != SystemType), "Code", "Name");
                exDataBind(this.radl_Status, FeedbackCache.StatusDictionary, "Key", "Value");
                BindData(1, int.Parse(radl_Status.SelectedValue));
            }
        }

        private void exDataBind(ListControl control, object dataSource, string valueField, string textField)
        {
            control.DataSource = dataSource;
            control.DataValueField = valueField;
            control.DataTextField = textField;
            control.DataBind();
            control.SelectedIndex = 0;
            control.Items.Insert(0, new ListItem("全部", "9"));
            control.SelectedValue = "9";
        }

        private void BindData(int pageIndex, int selectIndex)
        {
            Expression<Func<TB_Feedback, bool>> f = null;
            if (selectIndex != 9)
            {
                f = (p => p.Type.StartsWith(SystemType) && p.Status == selectIndex);
            }
            else
            {
                f = (p => p.Type.StartsWith(SystemType) && p.Status >= 0);
            } 
            Expression<Func<TB_Feedback, int>> o = (p => p.ID);

            int records = 0;
            var feedList = DAL.DALHelper.Instance.GetList<TB_Feedback, int>(f, o, false, this.Pager1.PageSize, pageIndex, out records);
            this.Pager1.RecordCount = records;
            this.Pager1.InitControl();

            List<TB_Feedback> fList = new List<TB_Feedback>();
            if (feedList != null && feedList.Count > 0)
            {
                List<string> userKeys = feedList.Select(c => c.UserIdentity).ToList();
                Expression<Func<TB_PublicUser, bool>> m = (p => userKeys.Contains(p.EMail) && p.Satus);
                Expression<Func<TB_PublicUser, string>> n = (p => p.EMail);
                var userList = DAL.DALHelper.Instance.GetList<TB_PublicUser, string>(m, n, true, 100, 1, out records);
                int i = 1;

                foreach (TB_Feedback feed in feedList)
                {
                    TB_PublicUser user = userList.Where(c => c.EMail == feed.UserIdentity).FirstOrDefault();
                    if (user != null)
                    {
                        feed.Sequence = i++;
                        feed.IdentityName = user.Name;
                        feed.Telephone = user.Telephone;
                        feed.Email = user.EMail;
                        TB_SystemMap map = SystemMapCache.Instance.List.Where(oa => oa.Code == feed.Type).FirstOrDefault();
                        if (map != null)
                        {
                            feed.TypeName = map.Name;
                        }
                        fList.Add(feed);
                    }
                }
            }
            this.rpt_Comp.DataSource = fList;
            this.rpt_Comp.DataBind();
        }

        public void BindSelectedPageIndexChanged(int pageIndex, int nextPage)
        {
            BindData(nextPage, int.Parse(radl_Status.SelectedValue));
        }

        protected void btn_Delete_Click(object sender, EventArgs e)
        {
            int newsID = 0;
            if (!string.IsNullOrWhiteSpace(this.hid_ID.Value) && int.TryParse(this.hid_ID.Value, out newsID))
            {
                DAL.DALHelper.Instance.DeleteWithTransaction<TB_Feedback>("ID", newsID);
                BindData(1, int.Parse(radl_Status.SelectedValue));
            }
            this.hid_ID.Value = string.Empty;
        }
        protected void radl_Status_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindData(1, int.Parse(radl_Status.SelectedValue));
        }

		protected void btn_NewsRefresh_Click(object sender, EventArgs e)
		{
			BindData(this.Pager1.CurrentPageIndex, int.Parse(radl_Status.SelectedValue));
		}
    }
}